package com.hjimi.ski.presenter;

import com.hjimi.ski.bean.DeviceVideoBean;
import com.hjimi.ski.callback.BaseCallback;
import com.hjimi.ski.config.UrlConfig;
import com.hjimi.ski.iView.DeviceVideoIView;
import com.hjimi.ski.model.base.DataModel;
import com.hjimi.ski.model.base.ModelToken;
import com.hjimi.ski.utils.JsonParseUtil;
import com.hjimi.ski.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created By 孙岩
 * on 2023/2/11
 */
public class DeviceVideoPresenter extends BasePresenter<DeviceVideoIView>{
    @SuppressWarnings("unchecked")
    public void getDeviceVideo(int id){
        if(isViewAttached()){
            String url = UrlConfig.deviceVideo + id;
            DataModel.request(ModelToken.GET_REQUEST)
                    .url(url)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    List<DeviceVideoBean.DataBean> list = new ArrayList<>();
                                    JSONArray array = new JSONArray(obj.getString("data"));
                                    for(int i=0;i<array.length();i++){
                                        JSONObject object = new JSONObject(array.get(i).toString());

                                        DeviceVideoBean.DataBean bean = new DeviceVideoBean.DataBean(
                                                JsonParseUtil.getString(object,"name"),
                                                JsonParseUtil.getString(object,"thumbUrl"),
                                                JsonParseUtil.getString(object,"videoUrl")
                                        );
                                        list.add(bean);
                                    }
                                    getView().showDeviceVideo(list);
                                }else if(obj.getInt("code")==202){
                                    getView().showDeviceVideo(null);
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showDeviceVideo(null);
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
